using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class AudioManager : MonoBehaviour
{
    private static AudioManager instance;
    public static AudioManager Instance => instance;

    [SerializeField]
    private AudioSource bgmAS;

    [SerializeField]
    private AudioSource soundAS;

    [SerializeField]
    private AudioClip mainMenuClip;

    [SerializeField]
    private AudioClip gameClip;

    [SerializeField]
    private AudioClip victoryClip;

    [SerializeField]
    private AudioClip normalBtnClip;

    /// <summary>
    /// 播放主界面BGM
    /// </summary>
    public void PlayerMainMenuBGM()
    {
        if (bgmAS.clip == mainMenuClip)
            return;
        bgmAS.clip = mainMenuClip;
        bgmAS.Play();
    }

    /// <summary>
    /// 播放游戏中BGM
    /// </summary>
    public void PlayerGameBGM()
    {
        if (bgmAS.clip == gameClip)
            return;
        bgmAS.clip = gameClip;
        bgmAS.Play();
    }

    /// <summary>
    /// 播放通关音效
    /// </summary>
    public void PlayVictoryClip()
    {
        soundAS.clip = victoryClip;
        soundAS.Play();
    }

    /// <summary>
    /// 播放普通按钮音效
    /// </summary>
    public void PlayNormalBtnClip()
    {
        soundAS.clip = normalBtnClip;
        soundAS.Play();
    }

    private void Awake()
    {
        if (instance == null)
            instance = this;
    }

    /// <summary>
    /// 读取音乐信息
    /// </summary>
    public void ReadMusicInfo()
    {
        float bgmVolume = PlayerPrefs.GetFloat("BgmVolume", 0.2f);
        float soundVolume = PlayerPrefs.GetFloat("SoundVolume", 0.2f);
        bgmAS.volume = bgmVolume;
        soundAS.volume = soundVolume;
    }

    /// <summary>
    /// 设置BGM音量
    /// </summary>
    /// <param name="varVolume">音量</param>
    public void SetBgmVolume(float varVolume)
    {
        bgmAS.volume = varVolume;
    }

    /// <summary>
    /// 设置音效音量
    /// </summary>
    /// <param name="varVolume">音量</param>
    public void SetSoundVolume(float varVolume)
    {
        soundAS.volume = varVolume;
    }

    /// <summary>
    /// 存储声音设置
    /// </summary>
    public void SaveMusciSetting()
    {
        PlayerPrefs.SetFloat("BgmVolume", bgmAS.volume);
        PlayerPrefs.SetFloat("SoundVolume", soundAS.volume);
        PlayerPrefs.Save();
    }
}
